// WLMania.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <iomanip>
#include <atlbase.h>

#import "C:\\Program Files\\Windows Live\\Messenger\\msnmsgr.exe" tlbid(2) //no_smart_pointers

using namespace std;

void experiment()
{
	CComPtr<MessengerAPI::IMessenger> msgr;
	HRESULT hr = msgr.CoCreateInstance(__uuidof(MessengerAPI::Messenger), NULL, CLSCTX_LOCAL_SERVER);
	cout << "Create Messenger: 0x" << hex  << hr << endl;
	if (FAILED(hr)) return;

	try {
		MessengerAPI::MISTATUS status = msgr->GetMyStatus();
		cout << "My status: 0x" << hex << status << endl;
		if (status == MessengerAPI::MISTATUS_OFFLINE) return;

		CComQIPtr<MessengerAPI::IMessengerContacts> contacts = msgr->GetMyContacts();
		if (!contacts) {
			cout << "Wrong type of MyContacts" << endl;
			return;
		}

		long count = contacts->GetCount();
		cout << endl << count << " contacts total." << endl;

		for (int i = 0; i < count; ++i) {
			CComQIPtr<MessengerAPI::IMessengerContact> contact = contacts->Item(i);
			if (!contact) {
				cout << "Wrong type Contact" << endl;
				return;
			}

			cout << CW2A((BSTR)contact->GetFriendlyName()) << endl;
			cout << CW2A((BSTR)contact->GetSigninName()) << endl;
			cout << CW2A((BSTR)contact->GetServiceId()) << endl;
			cout << endl;

		}
	} catch (_com_error &err) {
	}
}

int _tmain(int argc, _TCHAR* argv[])
{
	CoInitialize(NULL);
	experiment();
	CoUninitialize();
	return 0;
}

